package lowijs.XfDroid;

import java.text.*;
import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;
import lowijs.XfireClient.util.*;

public class Screenshots extends ListActivity implements XfireObserver {
	private ScreenshotsAdapter adapter = null;
	
	private ProgressDialog pd = null;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.screenshots);
		
		setTitle("XfDroid : " + getString(R.string.screenshots_for) + " "
				+ getIntent().getStringExtra("nickname"));
	}
	
	public void onStart() {
		super.onStart();
		EventManager.addObserver(this);
		
		if (adapter == null)
			showProgressDialog();
	}
	
	public void onSaveInstanceState(Bundle b) {
		b.putBoolean("pd", pd != null && pd.isShowing());
		super.onSaveInstanceState(b);
	}
	
	public void onStop() {
		super.onStop();
		EventManager.removeObserver(this);		
		
		if (pd != null)
			pd.dismiss();
	}
	
	public void onRestoreInstanceState(Bundle b) {
		if (b.getBoolean("pd")) {
			showProgressDialog();
			b.remove("pd");
		}
		super.onRestoreInstanceState(b);
	}
	
	public void onListItemClick(ListView l, View v, int position, long id) {
		Screenshot ss = adapter.getItem(position);
		
		Intent i = new Intent(this, ViewScreenshot.class);
		i.putExtra("guid", ss.getGuid());
		startActivity(i);
		
		super.onListItemClick(l, v, position, id);
	}
	
	private void showProgressDialog() {
		pd = new ProgressDialog(this);
		pd.setTitle(R.string.please_wait);
		pd.setMessage(getString(R.string.getting_screenshots));
		pd.setCancelable(false);
		pd.show();
	}
	
	public void handleEvent(XfireEvent event) {
		switch(event.getEventType()) {
		case XfireEvent.XF_SCREENSHOTS:
			ScreenshotsEvent se = (ScreenshotsEvent)event;
			pd.dismiss();
			if (se.getScreenshots() == null)
				return;
			adapter = new ScreenshotsAdapter(this, R.layout.screenshotrow,
					se.getScreenshots());
			screenshotsHandler.sendEmptyMessage(0);

			return;
		}
	}
	
	private Handler screenshotsHandler = new Handler() {
		public void handleMessage(Message m) {
			adapter.sort(new ScreenshotComparator());
			setListAdapter(adapter);
		}
	};
	
	private class ScreenshotsAdapter extends ArrayAdapter<Screenshot> {
		public ScreenshotsAdapter(Context ctx, int resId,
				List<Screenshot> list) {
			super(ctx, resId, list);
		}
		
		public View getView(int pos, View v, ViewGroup parent) {
			if (v == null) {
				LayoutInflater infl = (LayoutInflater)getSystemService(
						LAYOUT_INFLATER_SERVICE);
				v = infl.inflate(R.layout.screenshotrow, null);
			}
			
			Screenshot ss = getItem(pos);
			
			TextView tvDescription = (TextView)v.findViewById(
					R.id.tvDescription);
			TextView tvGamename = (TextView)v.findViewById(R.id.tvGamename);
			TextView tvTime = (TextView)v.findViewById(R.id.tvTime);
			
			tvDescription.setText(ss.getDescription());
			tvGamename.setText(ss.getGamename());
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, HH:mm");
			tvTime.setText(sdf.format(ss.getDate()));
			
			return v;
		}
	}
}
